package org.example.过滤消息.消费;

import lombok.SneakyThrows;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.MessageSelector;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;

/**
 * @Auther:Liu
 * @Testname:过滤消息消费
 * @Date:2025/8/22 18:45
 */
public class 过滤消息消费 {
    @SneakyThrows
    public static void main(String[] args) {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_group_c");
        consumer.setNamesrvAddr("192.168.10.88:9876");
        consumer.subscribe("topic_a", MessageSelector.bySql("age>50 and gender='女'"));
        consumer.registerMessageListener((MessageListenerConcurrently) (list, consumeConcurrentlyContext) -> {
            try {
                for (MessageExt messageExt : list) {
                    String body = new String(messageExt.getBody(), StandardCharsets.UTF_8);
                    System.out.println(body);
                }
            } catch (Exception e) {
                System.out.println("出现了异常信息，重写消费");
                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });
        consumer.start();
    }
}